
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using Apps.IDAL.PL;
using Apps.Models.PL;
using Apps.IBLL.PL;
using LinqKit;

namespace Apps.BLL.PL
{
    public partial class V_PL_BATCH_FOR_TESTBLL : Virtual_V_PL_BATCH_FOR_TESTBLL, IV_PL_BATCH_FOR_TESTBLL
    {
    }
    public class Virtual_V_PL_BATCH_FOR_TESTBLL
    {
        [Dependency]
        public IV_PL_BATCH_FOR_TESTRepository m_Rep { get; set; }

        public virtual List<V_PL_BATCH_FOR_TESTModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<V_PL_BATCH_FOR_TEST> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
                                a => a.BATCH_CODE.Contains(queryStr)

                                || a.ARTIFICIAL_CODE.ToString().Contains(queryStr)
                                || a.TEST_CODE.Contains(queryStr)



                                );
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

        public virtual List<V_PL_BATCH_FOR_TESTModel> GetPagerList(ref GridPager pager, Expression<Func<V_PL_BATCH_FOR_TEST, bool>> whereLambda)
        {
            try
            {
                IQueryable<V_PL_BATCH_FOR_TEST> queryData = m_Rep.GetList().Where(whereLambda.Compile()).AsQueryable();//.AsExpandable().Where(whereLambda);

                pager.totalRows = queryData.Count();
                //排序
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return CreateModelList(ref queryData);
            }
            catch (Exception )
            {
                return null;
            }
        }

        public virtual List<V_PL_BATCH_FOR_TESTModel> GetList(Expression<Func<V_PL_BATCH_FOR_TEST, bool>> whereLambda)
        {

            IQueryable<V_PL_BATCH_FOR_TEST> queryData = m_Rep.GetList(whereLambda);

            return CreateModelList(ref queryData);
        }

        public virtual List<V_PL_BATCH_FOR_TESTModel> GetListByParentId(ref GridPager pager, string queryStr, object parentId)
        {
            return new List<V_PL_BATCH_FOR_TESTModel>();
        }

        public List<V_PL_BATCH_FOR_TESTModel> CreateModelList(ref IQueryable<V_PL_BATCH_FOR_TEST> queryData)
        {
            List<V_PL_BATCH_FOR_TESTModel> modelList = (from r in queryData
                                                        select new V_PL_BATCH_FOR_TESTModel
                                                        {
                                                            PLAN_DATE = r.PLAN_DATE,
                                                            BATCH_CODE = r.BATCH_CODE,
                                                            ARTIFICIAL_CODE = r.ARTIFICIAL_CODE,
                                                            TEST_CODE = r.TEST_CODE,
                                                            MINE_ID = r.MINE_ID,
                                                            MINE_NAME = r.MINE_NAME,
                                                            MATERIEL_ID = r.MATERIEL_ID,
                                                            MATERIEL_NAME = r.MATERIEL_NAME,
                                                            STATISTICS = r.STATISTICS,
                                                            SUPPLIER_ID = r.SUPPLIER_ID,
                                                            SUPPLIER_NAME = r.SUPPLIER_NAME,
                                                            TRANSPORT_ID = r.TRANSPORT_ID,
                                                            TRANSPORT_NAME = r.TRANSPORT_NAME,
                                                            BEGIN_TIME = r.BEGIN_TIME,
                                                            END_TIME = r.END_TIME,
                                                            AMOUNT = r.AMOUNT,
                                                            QNET = r.QNET,
                                                            ST = r.ST,
                                                            REMARKS = r.REMARKS,
                                                            MAKE_CODE = r.MAKE_CODE,
                                                            MAKE_MODE_STR = r.MAKE_MODE_STR,
                                                            EQUIPMENT_NAME = r.EQUIPMENT_NAME,
                                                            CARCOUNT = r.CARCOUNT
                                                        }).ToList();

            return modelList;
        }
        

        public virtual bool Create(ref ValidationErrors errors, V_PL_BATCH_FOR_TESTModel model)
        {
            try
            {
                V_PL_BATCH_FOR_TEST entity = m_Rep.GetById(model.BATCH_CODE);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new V_PL_BATCH_FOR_TEST();
              //  entity.PLAN_ID = model.PLAN_ID;
                //entity.PLAN_DATE = model.PLAN_DATE;
                //entity.PLAN_USER_ID = model.PLAN_USER_ID;
                //entity.CREATE_TIME = model.CREATE_TIME;
                //entity.PUBLISHED = model.PUBLISHED;
                //entity.PUBLISH_TIME = model.PUBLISH_TIME;


                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }


        public virtual bool Deletes(ref ValidationErrors errors, Expression<Func<V_PL_BATCH_FOR_TEST, bool>> whereLambda)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {

                    foreach (var entity in db.V_PL_BATCH_FOR_TEST.Where(whereLambda))
                    {
                        db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
                    }

                    if (db.SaveChanges() > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }




        public virtual bool Edit(ref ValidationErrors errors, V_PL_BATCH_FOR_TESTModel model)
        {
            try
            {
                V_PL_BATCH_FOR_TEST entity = m_Rep.GetById(model.BATCH_CODE);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                entity.BATCH_CODE = model.BATCH_CODE;
              


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }



        public virtual V_PL_BATCH_FOR_TESTModel GetById(object id)
        {
            if (IsExists(id))
            {
                V_PL_BATCH_FOR_TEST entity = m_Rep.GetById(id);
                V_PL_BATCH_FOR_TESTModel model = new V_PL_BATCH_FOR_TESTModel();
                model.PLAN_DATE = entity.PLAN_DATE;
              
                return model;
            }
            else
            {
                return null;
            }
        }

        public virtual V_PL_BATCH_FOR_TESTModel GetItem(Expression<Func<V_PL_BATCH_FOR_TEST, bool>> whereLambda)
        {

            V_PL_BATCH_FOR_TEST entity = m_Rep.GetItem(whereLambda);
            if (entity == null) return null;

            V_PL_BATCH_FOR_TESTModel model = new V_PL_BATCH_FOR_TESTModel();
            model.PLAN_DATE = entity.PLAN_DATE;
         
            return model;
        }


        /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<V_PL_BATCH_FOR_TESTModel> list, ref ValidationErrors errors)
        {

            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
            excelFile.AddMapping<V_PL_BATCH_FOR_TESTModel>(x => x.PLAN_DATE, "PLAN_DATE");
            //excelFile.AddMapping<V_PL_BATCH_FOR_TESTModel>(x => x.PLAN_USER_ID, "PLAN_USER_ID");
            //excelFile.AddMapping<V_PL_BATCH_FOR_TESTModel>(x => x.CREATE_TIME, "CREATE_TIME");
            //excelFile.AddMapping<V_PL_BATCH_FOR_TESTModel>(x => x.PUBLISHED, "PUBLISHED");
            //excelFile.AddMapping<V_PL_BATCH_FOR_TESTModel>(x => x.PUBLISH_TIME, "PUBLISH_TIME");

            //SheetName
            var excelContent = excelFile.Worksheet<V_PL_BATCH_FOR_TESTModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new V_PL_BATCH_FOR_TESTModel();
               // entity.PLAN_ID = row.PLAN_ID;
                entity.PLAN_DATE = row.PLAN_DATE;
                //entity.PLAN_USER_ID = row.PLAN_USER_ID;
                //entity.CREATE_TIME = row.CREATE_TIME;
                //entity.PUBLISHED = row.PUBLISHED;
                //entity.PUBLISH_TIME = row.PUBLISH_TIME;

                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<V_PL_BATCH_FOR_TESTModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        V_PL_BATCH_FOR_TEST entity = new V_PL_BATCH_FOR_TEST();
                      //  entity.PLAN_ID = model.PLAN_ID;
                     //   entity.PLAN_DATE = model.PLAN_DATE;
                        //entity.PLAN_USER_ID = model.PLAN_USER_ID;
                        //entity.CREATE_TIME = model.CREATE_TIME;
                        //entity.PUBLISHED = model.PUBLISHED;
                        //entity.PUBLISH_TIME = model.PUBLISH_TIME;

                        db.V_PL_BATCH_FOR_TEST.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
        public void Dispose()
        {

        }

    }
}
